package com.manli.api.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.manli.api.dto.AQiniuFiles;
import com.manli.api.service.AQiniuFilesService;
import com.manli.common.utils.qiniu.QueryParamsFileKey;
import com.manli.common.utils.qiniu.QueryParamsQiniu;
import com.manli.common.utils.qiniu.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.manli.api.base.annotation.Permissions;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;

import io.swagger.annotations.Api;

/**
 * 
 *
 * @author manli
 * @email 
 * @date 2020-04-29 10:07:53
 */
@Api(value="/qiniu", tags="文件上传")
@RestController
@RequestMapping("api/qiniu")
public class QiniuController {
    @Autowired
    private AQiniuFilesService aQiniuFilesService;
	//获取七牛token
	@PostMapping("/getQiniuToken")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result getQiniuToken() throws IOException  {  
		return ResultUtil.success(QiniuUploadUtils.getToken(QiniuConfig.accessKey, QiniuConfig.secretKey, QiniuConfig.bucketShinehaopic));
	}
	//文件上传
    @ApiOperation(value = "批量文件上传",notes = "参数名file")
	@PostMapping("/uploadFile")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result uploadFile(@RequestParam("file") MultipartFile files[],HttpServletRequest request) throws IOException {
		List list = UploadFilesUtil.loadFileByQiniu4BucketShinehaopic(files,request);
		return ResultUtil.success(list);
	}

	//图片url，保存七牛
	@ApiOperation(value = "图片url，保存七牛",notes = "参数名file")
	@PostMapping("/imgUrluploadFile")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_OPEN)
	public Result imgUrluploadFile(@RequestBody QueryParamsImgUrlUpload urls) throws Exception {
		Map map = UploadFilesUtil.imgUrluploadFile(urls);
        List<Map> list = (List<Map>)map.get("nameList");
        //填加已经上传到七牛的文件url到自己的上传表
        list.forEach(map1 -> {
            AQiniuFiles aQiniuFiles = new AQiniuFiles();
            aQiniuFiles.setFileName(map1.get("fileName").toString());
            aQiniuFiles.setQiqiuUrl(map1.get("url").toString());
			aQiniuFiles.setFileSize(Long.parseLong(map1.get("size").toString()));
			aQiniuFiles.setMimeType(map1.get("type").toString());
            aQiniuFilesService.save(aQiniuFiles);
        });
		return ResultUtil.success(map.get("list"));
	}

	//文件上传
    @ApiOperation(value = "单文件上传",notes = "参数名file")
	@PostMapping("/uploadOneFile")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
		List list = UploadFilesUtil.loadFileByQiniu4tpic(file,request);
		return ResultUtil.success(list);
	}

	//文件上传
	@PostMapping("/uploadFileByBase64")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result uploadFileByBase64(HttpServletRequest request , String base64content) throws IOException  {  
		List list = UploadFilesUtil.loadBase64FileByQiniu4BucketShinehaopic(base64content, "base64Img",request);
		return ResultUtil.success(list);
	}

	//获取空间文件列表
//	@ApiOperation(value = "获取空间文件列表(不分页)",notes = "")
	@PostMapping("/getFileList")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result getFileList(@RequestBody QueryParamsQiniu queryParamsQiniu){
        List<QiNiuFileInfo> list = UploadFilesUtil.getFileList(queryParamsQiniu);
		return ResultUtil.success(list);
	}

	//获取空间文件列表
//	@ApiOperation(value = "获取空间文件列表(分页)，暂不返回总行数",notes = "")
	@PostMapping("/getFileListPaged")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result getFileListPaged(@RequestBody QueryParamsQiniu queryParamsQiniu){
		if(queryParamsQiniu.getPage() == null || queryParamsQiniu.getLimit() == null){
			return ResultUtil.error(-1,"page,limit缺失");
		}
		List<QiNiuFileInfo> list = UploadFilesUtil.getFileListPaged(queryParamsQiniu);
		return ResultUtil.success(list);
	}

	//删除空间文件
//	@ApiOperation(value = "删除空间文件",notes = "")
	@PostMapping("/deleteFile")
	@Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
	public Result deleteFile(@RequestBody QueryParamsFileKey queryParamsFileKey)throws Exception{
		UploadFilesUtil.deleteFile(queryParamsFileKey);
		return ResultUtil.success();
	}
}
